package server

import (
	v1 "medicine/api/helloworld/v1"
	medicinev1 "medicine/api/medicine/v1"
	"medicine/internal/conf"
	"medicine/internal/data"
	"medicine/internal/service"

	"github.com/go-kratos/kratos/v2/log"
	"github.com/go-kratos/kratos/v2/middleware/recovery"
	"github.com/go-kratos/kratos/v2/transport/http"
)

// NewHTTPServer new an HTTP server.
func NewHTTPServer(c *conf.Server,
	greeter *service.GreeterService,
	product *service.ProductService,
	seckill *service.SeckillService,
	seckillOrder *service.SeckillOrderService,
	category *service.CategoryService,
	brand *service.BrandService,
	user *service.UserService,
	points *service.PointsService,
	logger log.Logger) *http.Server {
	var opts = []http.ServerOption{
		http.Middleware(
			recovery.Recovery(),
		),
	}
	if c.Http.Network != "" {
		opts = append(opts, http.Network(c.Http.Network))
	}
	if c.Http.Addr != "" {
		opts = append(opts, http.Address(c.Http.Addr))
	}
	if c.Http.Timeout != nil {
		opts = append(opts, http.Timeout(c.Http.Timeout.AsDuration()))
	}
	srv := http.NewServer(opts...)

	// 注册所有服务
	v1.RegisterGreeterHTTPServer(srv, greeter)
	medicinev1.RegisterProductHTTPServer(srv, product)
	medicinev1.RegisterSeckillHTTPServer(srv, seckill)
	medicinev1.RegisterSeckillOrderHTTPServer(srv, seckillOrder)
	medicinev1.RegisterCategoryHTTPServer(srv, category)
	medicinev1.RegisterBrandHTTPServer(srv, brand)
	medicinev1.RegisterUserHTTPServer(srv, user)
	medicinev1.RegisterPointsHTTPServer(srv, points)

	alipayService := service.NewAlipayService(data.GlobalDataRepo, points, seckillOrder.Souc, logger)
	srv.Route("/").POST("/api/alipay/notify", alipayService.AlipayNotifyKratos)

	return srv
}
